//
//  HNaviBar.m
//  BagsProject
//
//  Created by sunsoft on 2017/3/14.
//  Copyright © 2017年 sunnsoft. All rights reserved.
//

#import "HNaviBar.h"
@interface HNaviBar ()

@property (nonatomic,copy) void (^leftEventBlock)(UIButton * leftSender);
@property (nonatomic,copy) void (^rightEventBlock)(UIButton * rightSender);
@property (nonatomic,strong) CALayer * lineLayer;

@end

@implementation HNaviBar

+ (instancetype)customNaviBarWithTitle:(NSString *)title aimView:(UIView *)aimView
{
    HNaviBar * hNaviBar = [self customNaviBarWithTitle:title andLeftEvent:nil aimView:aimView];
    return hNaviBar;
}

+(instancetype)customNaviBarWithTitle:(NSString *)title andLeftEvent:(void(^)(UIButton * leftSender))event aimView:(UIView *)aimView{
    
    HNaviBar * hNavibar = [[HNaviBar alloc] initWithFrame:CGRectMake(0, 0, HHScreenWidth, 64) withTitle:title AndLeftBlock:event];
    
    if (aimView) {
        [aimView addSubview:hNavibar];
    }
    return hNavibar;
}

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title AndLeftBlock:(void(^)(UIButton * leftSender))leftBlock
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor blackColor]];
        self.leftEventBlock = leftBlock;
        [self initUI];
        [self.titleLabel setText:title];
    }
    return  self;
}
-(void)initUI{
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 18, HHScreenWidth - 100, 40)];
    //    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [self addSubview:self.titleLabel];
    
    self.leftButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 18, 50, 44)];
    [self.leftButton setImage:[UIImage imageNamed:@"icon-wite_Back"] forState:UIControlStateNormal];
    [self.leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    [self.leftButton addTarget:self action:@selector(eventButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.leftButton setTag:1];
    if (self.leftEventBlock) {
        [self.leftButton setHidden:NO];
    }
    else [self.leftButton setHidden:YES];
    [self addSubview:self.leftButton];
    
    
    self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake( CGRectGetWidth(self.frame) - 65, 18, 55, 44)];
    [self.rightButton setTag:2];
    [self.rightButton addTarget:self action:@selector(eventButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightButton setHidden:NO];
    [self addSubview:self.rightButton];
    
//    self.lineLayer = [CALayer layer];
//    [self.lineLayer setFrame:CGRectMake(0, 63, HHScreenWidth, 1)];
//    [self.lineLayer setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.7].CGColor];
//    [self.layer addSublayer:self.lineLayer];
}
- (void)eventButtonTaped:(UIButton *)button
{
    if(button.tag == 1){
        if (self.leftEventBlock) {
            self.leftEventBlock(self.leftButton);
        }
    }
    else {
        if (self.rightEventBlock) {
            self.rightEventBlock(self.rightButton);
        }
    }
}

- (void)setButtonImage:(UIImage *)image title:(NSString *)title andEvent:(void (^)(UIButton * sender))event isLeft:(BOOL)isLeft
{
    UIButton * button = isLeft ? self.leftButton : self.rightButton;
    if (isLeft) {
        self.leftEventBlock = event;
    }
    else self.rightEventBlock = event;
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
    }
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (event) {
        [button setHidden:NO];
    }
    else [button setHidden:YES];
}

- (void)setRightEvent:(void (^)(UIButton * sender))event
{
    if (event) {
        self.rightEventBlock = event;
        [self.rightButton setHidden:NO];
    }
}

- (void)setLeftEvent:(void (^)(UIButton * sender))event
{
    if (event) {
        self.leftEventBlock = event;
        [self.leftButton setHidden:NO];
    }
}

- (void)dealloc
{
    NSLog(@"bar dealloc");
}

@end
